package com.xuxueli.applyModules.utils.testJsonPathFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jayway.jsonpath.JsonPath;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * 测试jsonpath值
 */
public class TestJsonPath {

    public static void main(String[] args) {
        String value = "{\n" +
                "    \"payload\": {\n" +
                "        \"total\": 227978,\n" +
                "        \"size\": 100,\n" +
                "        \"page\": 1,\n" +
                "        \"objects\": [\n" +
                "            {\n" +
                "                \"objectId\": \"1XXXXXXXX\",\n" +
                "                \"attributes\": {\n" +
                "                    \"attr_whcd\": \"研究生教育\",\n" +
                "                    \"attr_sg\": null,\n" +
                "                    \"attr_xm\": \"XXXXXXX\",\n" +
                "                    \"attr_csrq\": \"1994-11-12\",\n" +
                "                    \"attr_hjdz_qhnxxdz\": \"XXXXXXX\",\n" +
                "                    \"attr_hjdz_ssxq\": \"市辖区\",\n" +
                "                    \"attr_zy\": null,\n" +
                "                    \"attr_zzmm\": null,\n" +
                "                    \"attr_csd\": \"东城区\",\n" +
                "                    \"attr_jg\": \"XXXXXX\",\n" +
                "                    \"attr_bsxqtzz\": null,\n" +
                "                    \"attr_hyzk\": \"未婚\",\n" +
                "                    \"attr_hkxz\": \"1\",\n" +
                "                    \"attr_yhzgx\": \"女\",\n" +
                "                    \"attr_person_age\": 29,\n" +
                "                    \"attr_zjxy\": null,\n" +
                "                    \"attr_mz\": \"汉族\",\n" +
                "                    \"attr_xb\": \"女\"\n" +
                "                },\n" +
                "                \"tags\": {\n" +
                "                    \"tag_tx\": null,\n" +
                "                    \"tag_gdjy\": \"高等教育\",\n" +
                "                    \"tag_national_minority\": null,\n" +
                "                    \"tag_gyyq\": null\n" +
                "                },\n" +
                "                \"metrics\": {\n" +
                "                    \"metric_zyjsrzcs\": null,\n" +
                "                    \"metric_gzqys\": 0,\n" +
                "                    \"metric_zyzcs\": 3,\n" +
                "                    \"metric_jdcwgs\": null,\n" +
                "                    \"metric_xzcf\": null\n" +
                "                }\n" +
                "            },\n" +
                "            {\n" +
                "                \"objectId\": \"XXXXXXXXX\",\n" +
                "                \"attributes\": {\n" +
                "                    \"attr_whcd\": \"其他\",\n" +
                "                    \"attr_sg\": null,\n" +
                "                    \"attr_xm\": \"XXXXXXX\",\n" +
                "                    \"attr_csrq\": \"2002-10-15\",\n" +
                "                    \"attr_hjdz_qhnxxdz\": \"XXXXX\",\n" +
                "                    \"attr_hjdz_ssxq\": \"市辖区\",\n" +
                "                    \"attr_zy\": null,\n" +
                "                    \"attr_zzmm\": null,\n" +
                "                    \"attr_csd\": \"苏州市\",\n" +
                "                    \"attr_jg\": \"XXXX\",\n" +
                "                    \"attr_bsxqtzz\": null,\n" +
                "                    \"attr_hyzk\": \"未婚\",\n" +
                "                    \"attr_hkxz\": \"1\",\n" +
                "                    \"attr_yhzgx\": \"子\",\n" +
                "                    \"attr_person_age\": 21,\n" +
                "                    \"attr_zjxy\": null,\n" +
                "                    \"attr_mz\": \"汉族\",\n" +
                "                    \"attr_xb\": \"男\"\n" +
                "                },\n" +
                "                \"tags\": {\n" +
                "                    \"tag_tx\": null,\n" +
                "                    \"tag_gdjy\": null,\n" +
                "                    \"tag_national_minority\": null,\n" +
                "                    \"tag_gyyq\": null\n" +
                "                },\n" +
                "                \"metrics\": {\n" +
                "                    \"metric_zyjsrzcs\": null,\n" +
                "                    \"metric_gzqys\": 0,\n" +
                "                    \"metric_zyzcs\": 1,\n" +
                "                    \"metric_jdcwgs\": null,\n" +
                "                    \"metric_xzcf\": null\n" +
                "                }\n" +
                "            }\n" +
                "            \n" +
                "        ],\n" +
                "        \"sql\": \"select object_registered_residence.subject_id as subject_id , object_registered_residence.subject_id as subject_id , object_registered_residence.attr_xm as attr_xm , object_registered_residence.attr_hkxz as attr_hkxz , object_registered_residence.attr_yhzgx as attr_yhzgx , object_registered_residence.attr_xb as attr_xb , object_registered_residence.attr_csd as attr_csd , object_registered_residence.attr_mz as attr_mz , object_registered_residence.attr_jg as attr_jg , object_registered_residence.attr_csrq as attr_csrq , object_registered_residence.attr_bsxqtzz as attr_bsxqtzz , object_registered_residence.attr_zjxy as attr_zjxy , object_registered_residence.attr_sg as attr_sg , object_registered_residence.attr_whcd as attr_whcd , object_registered_residence.attr_hyzk as attr_hyzk , object_registered_residence.attr_zy as attr_zy , object_registered_residence.attr_zzmm as attr_zzmm , object_registered_residence.attr_hjdz_ssxq as attr_hjdz_ssxq , object_registered_residence.attr_hjdz_qhnxxdz as attr_hjdz_qhnxxdz , object_registered_residence.attr_person_age as attr_person_age , object_registered_residence.tag_national_minority as tag_national_minority , object_registered_residence.tag_tx as tag_tx , object_registered_residence.tag_gdjy as tag_gdjy , object_registered_residence.tag_gyyq as tag_gyyq , object_registered_residence.metric_gzqys as metric_gzqys , object_registered_residence.metric_zyzcs as metric_zyzcs , object_registered_residence.metric_zyjsrzcs as metric_zyjsrzcs , object_registered_residence.metric_jdcwgs as metric_jdcwgs , object_registered_residence.metric_xzcf as metric_xzcf  from  datasense_4a0326012d9d401d9c130a09cc879079_znzs_dome.`object_registered_residence`  where (`tag_zn` in ('子女' )) order by subject_id asc  limit 0 , 100\"\n" +
                "    },\n" +
                "    \"code\": \"200\",\n" +
                "    \"msg\": \"ok\"\n" +
                "}";
        try {
            //输出payload节点中objects所有对象
            Object objects = JsonPath.read(value, "$.payload.objects[*]");
            Optional<Object> optionalObj = Optional.ofNullable(objects);
            if(optionalObj.isPresent()){
                if(optionalObj.get() instanceof JSON){
                    List<JSONObject> jsonObjectList = JSON.parseArray(optionalObj.get().toString(), JSONObject.class);
                    for (JSONObject jsonObject:jsonObjectList){
                        JSONObject attributes = jsonObject.getJSONObject("attributes");
                        System.out.println("attributes:{}"+JSONObject.toJSONString(attributes, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
                        JSONObject tags = jsonObject.getJSONObject("tags");
                        System.out.println("tags:{}"+JSONObject.toJSONString(tags, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
                        JSONObject metrics = jsonObject.getJSONObject("metrics");
                        System.out.println("metrics:{}"+JSONObject.toJSONString(metrics, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
                    }
                }
            }
            //输出payload节点中sql对象
            Object objectSql = JsonPath.read(value, "$.payload.sql");
            Optional<Object> optionalSql = Optional.ofNullable(objectSql);
            if(optionalSql.isPresent()){
                if(optionalSql.get() instanceof String){

                }
                System.out.println("sql语句："+optionalSql.get().toString());
            }
        }catch (Exception ex){
            System.out.println("解析数据失败");
        }
    }

    private static String getJsonValue() {
        String valueString = StringUtils.EMPTY;
        String bookJson = "{\n" +
                "  \"store\": {\n" +
                "    \"book\":[\n" +
                "      { \"category\": \"reference\",\n" +
                "        \"author\": \"Nigel Rees\",\n" +
                "        \"title\": \"Sayings of the Century\",\n" +
                "        \"price\": 8.95\n" +
                "      },\n" +
                "      { \"category\": \"fiction\",\n" +
                "        \"author\": \"J. R. R. Tolkien\",\n" +
                "        \"title\": \"The Lord of the Rings\",\n" +
                "        \"isbn\": \"0-395-19395-8\",\n" +
                "        \"price\": 22.99\n" +
                "      }\n" +
                "    ],\n" +
                "    \"bicycle\": {\n" +
                "      \"color\": \"red\",\n" +
                "      \"price\": 19.95\n" +
                "    }\n" +
                "  }\n" +
                "}\n";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //输出book节点中某一条数据
            Object reads = JsonPath.read(bookJson, "$.store.book[1].author");
            //输出json节点某一个对象
            Object readss = JsonPath.read(bookJson, "$.store.bicycle.color");
            //输出book节点中所有对象
            Object readsss = JsonPath.read(bookJson, "$.store.book[*]");
            //输出book节点中所有价格小于10的对象
            Object readssss = JsonPath.read(bookJson, "$.store.book[?(@.price<10)]");
            //输出book节点中所有含有isbn的对象
            Object readsssss = JsonPath.read(bookJson, "$.store.book[?(@.isbn)]");
            if(null!=reads){
                stringBuilder.append("输出book节点中某一条数据："+reads.toString()).append("\n");
            }
            Optional<Object> readss1 = Optional.ofNullable(readss);
            if(readss1.isPresent()){
                stringBuilder.append("输出json节点某一个对象："+readss1.get().toString()).append("\n");
            }
            Optional<Object> readsss1 = Optional.ofNullable(readsss);
            if(readsss1.isPresent()){
                stringBuilder.append("输出book节点中所有对象："+readsss1.get().toString()).append("\n");
            }
            Optional<Object> readsss1s = Optional.ofNullable(readssss);
            if(readsss1s.isPresent()){
                stringBuilder.append("输出book节点中所有价格小于10的对象："+readsss1s.get().toString()).append("\n");
            }
            Optional<Object> readsss1ss = Optional.ofNullable(readsssss);
            if(readsss1ss.isPresent()){
                stringBuilder.append("输出book节点中所有含有isbn的对象："+readsss1ss.get().toString()).append("\n");
            }
            if(null!=stringBuilder){
                valueString = stringBuilder.toString();
            }
        }catch (Exception ex){
            System.out.println("解析数据失败");
        }
        return valueString;
    }


}
